cmake_minimum_required(VERSION 3.8)
project(turtle_square)

if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# find dependencies
find_package(ament_cmake REQUIRED)
find_package(rclcpp REQUIRED)
find_package(rclcpp_action REQUIRED)
find_package(geometry_msgs REQUIRED)  # 如果需要turtle控制
find_package(turtlesim REQUIRED)
find_package(std_msgs REQUIRED)
find_package(std_srvs REQUIRED)
find_package(rosidl_default_generators REQUIRED)  #这个是自动生成message，service，action的代码的依赖
find_package(rosidl_default_runtime REQUIRED) # <-- 新增：运行时依赖，用于 C++ 头文件
find_package(rosidl_typesupport_cpp REQUIRED) 

#生成action接口
rosidl_generate_interfaces(${PROJECT_NAME}
  "action/RotateAndMove.action"
   DEPENDENCIES action_msgs
)
# 显式导出运行时依赖，确保其它包可以找到生成的类型
ament_export_dependencies(rosidl_default_runtime)
include_directories(
  include
  ${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp

)

if(BUILD_TESTING)
  find_package(ament_lint_auto REQUIRED)
  # the following line skips the linter which checks for copyrights
  # comment the line when a copyright and license is added to all source files
  set(ament_cmake_copyright_FOUND TRUE)
  # the following line skips cpplint (only works in a git repo)
  # comment the line when this package is in a git repo and when
  # a copyright and license is added to all source files
  set(ament_cmake_cpplint_FOUND TRUE)
  ament_lint_auto_find_test_dependencies()
endif()

set(_action_target "${PROJECT_NAME}__rosidl_generator_cpp")

#构建服务端可执行文件
add_executable(turtle_square_client src/turtle_square_client.cpp)

add_dependencies(turtle_square_client ${_action_target})

# 确保生成头文件目录可见
target_include_directories(turtle_square_client PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp)
ament_target_dependencies(turtle_square_client 
  rclcpp
  rclcpp_action
  geometry_msgs
  turtlesim
  std_msgs
  rosidl_default_runtime 
)
rosidl_target_interfaces(turtle_square_client ${PROJECT_NAME} "rosidl_typesupport_cpp")  # <-- 新增

add_executable(turtle_square_server src/turtle_square_server.cpp)
add_dependencies(turtle_square_server ${_action_target})
target_include_directories(turtle_square_server PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/rosidl_generator_cpp)
ament_target_dependencies(turtle_square_server
  rclcpp
  rclcpp_action
  geometry_msgs
  turtlesim
  std_msgs
  rosidl_default_runtime 
)

rosidl_target_interfaces(turtle_square_server ${PROJECT_NAME} "rosidl_typesupport_cpp")  # <-- 新增

install(TARGETS
  turtle_square_client
  turtle_square_server#你的可执行文件名
  DESTINATION lib/${PROJECT_NAME})

#安装action定义文件
install(DIRECTORY action
 DESTINATION share/${PROJECT_NAME})

ament_package()
